/*
 * gcode.h
 *
 * Created: 31.7.2013 20:55:39
 *  Author: Ferda
 */ 


#ifndef GCODE_H_
#define GCODE_H_

#include <stdlib.h>
#include <inttypes.h>

#include "Configuration.h"
#include "util.h"


typedef struct 
{
	uint8_t	maskl;
	uint8_t	maskh;
	
	// maskl values --------------------
	uint16_t N;		// 2
	uint16_t M;		// 2
	uint8_t	G;		// 1
	float X;		// 4
	float Y;		// 4
	float Z;		// 4
	float E;		// 4
	
	float F;		// 4
	uint8_t T;		// 1
	uint32_t S;		// 4
	uint32_t P;		// 4
	uint8_t v2mask; // 1
	
	
	uint8_t	intcmd; // 1
	char text[16];	// 16byte length
			
	uint16_t checkum;
} gcode_t;

#define HAS_N(maskl) (((maskl) & 1) != 0)
#define HAS_M(maskl) (((maskl) & 2) != 0)
#define HAS_G(maskl) (((maskl) & 4) != 0)
#define HAS_X(maskl) (((maskl) & 8) != 0)
#define HAS_Y(maskl) (((maskl) & 16) != 0)
#define HAS_Z(maskl) (((maskl) & 32) != 0)
#define HAS_E(maskl) (((maskl) & 64) != 0)
#define IS_ASCII(maskl) (((maskl) & 128) != 0)
//-----------------------------------------
#define HAS_F(maskh) (((maskh) & 1) != 0)
#define HAS_T(maskh) (((maskh) & 2) != 0)
#define HAS_S(maskh) (((maskh) & 4) != 0)
#define HAS_P(maskh) (((maskh) & 8) != 0)
#define HAS_V2(maskh) (((maskh) & 16) != 0) // readed but not used
// bit 5 is not used
#define HAS_INTERNAL(maskh) (((maskh) & 64) != 0)
#define HAS_TEXT(maskh) (((maskh) & 128) != 0)

//====================================================

#define IS_BINARY_CODE(maskl) (((maskl) & 0x80) == 0)
#define IS_TEXT_CODE(maskl) (((maskl) & 0x80) != 0)

//=====================================================

// compute buffer length in bytes for binary buffer data presented by masks
uint8_t get_binary_code_size(uint8_t maskl, uint8_t maskh);

// read content of binary buffer data to binary gcode
uint8_t* buffer_to_binary_code(uint8_t *buffer, gcode_t *ptr);

// store binary gcode to buffer
uint8_t* binary_code_to_buffer(gcode_t *ptr, uint8_t *buffer);




//==============================================================

#endif /* GCODE_H_ */